﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Core;
using SharedGenomics.Workbench.PresentationModel;

namespace SharedGenomics.Workbench.Models
{
    public class KeggPathwayFrequenciesModel : IKeggPathwayFrequenciesModel
    {
        private IList<KeggPathwayFrequency> _pathwayFrequencies;

        public KeggPathwayFrequenciesModel()
        {
            this.MaxFrequency = 0;
            this._pathwayFrequencies = new List<KeggPathwayFrequency>();
        }

        public void LoadPathwayFrequencies(IEnumerable<KeggPathwayFrequency> frequencies)
        {
            this._pathwayFrequencies.Clear();

            if (frequencies != null)
            {
                //sort input items in descending order of frequency
                List<KeggPathwayFrequency> pathwayList = frequencies.ToList();
                pathwayList.Sort((f1, f2) => f2.Frequency.CompareTo(f1.Frequency));

                

                foreach (KeggPathwayFrequency pwf in pathwayList)
                    this._pathwayFrequencies.Add(pwf);
            }

            //save max value - since the list is sorted the max value will be at index 0 if there are elements in the list
            this.MaxFrequency = this._pathwayFrequencies.Count > 0 ? this._pathwayFrequencies[0].Frequency : 0;

            this.PathwayFrequenciesLoaded.Raise(this);
        }

        public int MaxFrequency { get; private set; }

        public event EventHandler PathwayFrequenciesLoaded;

        #region IEnumerable<KeggPathwayFrequency> Members

        public IEnumerator<KeggPathwayFrequency> GetEnumerator()
        {
            return this._pathwayFrequencies.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}
